<?php
defined('PHP5_PHP') or die("Application is stopping!!!");
global $define;
checkLogin();
include($php5RootPath . '/classes/news.class' . $php5Ext);

$allow_news =1; 
$smarty->assign('allow_news', $allow_news);

$smarty->assign('link_view_news', sefBuild($php5WebPath, 'index.php?o=news&m=main&task=view', 0));
$smarty->assign('link_save_order', sefBuild($php5WebPath, 'index.php?o=news&m=main&task=order', 0));
$smarty->assign('link_save_news', sefBuild($php5WebPath, 'index.php?o=news&m=main&task=save', 0));
$smarty->assign('link_list_news', sefBuild($php5WebPath, 'index.php?o=news&m=main&task=list', 0));
$smarty->assign('link_edit_news', sefBuild($php5WebPath, 'index.php?o=news&m=main&task=edit', 0));
$smarty->assign('link_del_news', sefBuild($php5WebPath, 'index.php?o=news&m=main&task=delete', 0));
$smarty->assign('link_add_news', sefBuild($php5WebPath, 'index.php?o=news&m=main&task=add', 0));
$smarty->assign('path_upload_file', sefBuild($php5WebPath, 'index.php?o=upload_files&m=upload_file', 0));
$smarty->assign('link_add_cat', sefBuild($php5WebPath, 'index.php?o=news_category&m=create', 0));
$smarty->assign('main', '');
switch ($task){
	case 'view':
		viewNews();
		break;	
	case 'order':
		orderNews();
		break;
	case 'save':
		saveNews();
		break;
	case 'delete':
		deleteNews();
		break;
	case 'edit':
		editNews();
		break;
	case 'add':
		addNews();
		break;
	default:		
		listNews();		
		break;
}
function viewNews(){
	global $smarty, $php5RootAdminPath, $type, $php5Session, $php5WebPath, $php5TemplateAdminFile, $lang, $php5DB,$define, $php5RootPath,$php5Ext, $allow_news ;	
	$id = php5GetParam($_REQUEST, 'id', 0);	
	$row = new php5News($php5DB);
	$row->load($id);
	$row->post_date  = date('j M, Y', $row->post_date );
	$smarty->assign('row', $row);	
	// Category
	$php5DB->setQuery("SELECT name FROM #__category WHERE publish>0 AND type=2 AND id=".$row->category_id." ORDER BY `name` ASC");
	$cat_name = $php5DB->loadResult();	
	$smarty->assign('cat_name', $cat_name);			
	
	$smarty->assign('_NEWS', $lang["_NEWS"]);	
	$smarty->assign('_CATEGORY', $lang["_CATEGORY"]);
	$smarty->assign('_ADD_CATEGORY', $lang["_ADD_CATEGORY"]);
	$smarty->assign('_TITLE', $lang["_TITLE"]);
	$smarty->assign('_IMAGE', $lang["_IMAGE"]);
	$smarty->assign('_SHORT_DESCRIPTION', $lang["_SHORT_DESCRIPTION"]);
	$smarty->assign('_CONTENT', $lang["_CONTENT"]);
	$smarty->assign('_HIGHTLIGHT', $lang["_HIGHTLIGHT"]);
	$smarty->assign('_PUBLISHED', $lang["_PUBLISHED"]);
	$smarty->assign('_YES', $lang["_YES"]);
	$smarty->assign('_NO', $lang["_NO"]);
	
	// Message
	$smarty->assign('_MSS_NEWS_TITLE', $lang["_MSS_NEWS_TITLE"]);
	$smarty->assign('_MSS_NEWS_S_DESC', $lang["_MSS_NEWS_S_DESC"]);
	$smarty->assign('_MSS_NEWS_DESC', $lang["_MSS_NEWS_DESC"]);
	
	$smarty->assign('_UPDATE', $lang["_UPDATE"]);	
	$smarty->assign('_RESET', $lang["_RESET"]);	
	$smarty->assign('_BACK', $lang["_BACK"]);	
	
	$smarty->assign('link_save_user', sefBuild($php5WebPath, 'index.php?o=account&m=main&task=save', 0));
	if ($allow_news)
		$smarty->assign('main', $smarty->fetch(sprintf($php5TemplateAdminFile, 'en', $type , 'view.tpl')));
	else
		$smarty->assign('main', $smarty->fetch(sprintf($php5TemplateAdminFile, 'en', $type , 'view1.tpl')));	
}

function orderNews(){
	global $smarty, $php5RootAdminPath, $type, $php5Session, $php5WebPath, $php5TemplateAdminFile, $lang, $define, $php5DB, $php5RootPath;	
	global $msgAlert, $redirect;			
	if(!$define->checkAdminType($php5Session->getVar("admin_type"), "update_news")) {
		$msgAlert='Not allowed to use. Please try again.';
		$redirect = sefBuild($php5WebPath, 'index.php?o=news&m=main', 0);	
		return;
	}
	$order =php5GetParam($_REQUEST, 'ordering', 0);
	while (list($key, $value) = each($order)) {
		$row = new php5News($php5DB);
		$row->id = $key;
		$row->ordering = $value;
		if (!$row->store()) {
			$msg = "Error!!! Please try again!!!";		
		}
	}
	php5Redirect(sefBuild($php5WebPath, 'index.php?o=news&m=main', 0));	
}
function deleteNews(){
	global $smarty, $php5RootAdminPath, $type, $php5Session, $php5WebPath, $php5TemplateAdminFile, $lang, $define, $php5DB, $php5Ext, $php5RootPath;
	global $msgAlert, $redirect;			
	if(!$define->checkAdminType($php5Session->getVar("admin_type"), "update_news")) {
		$msgAlert='Not allowed to use. Please try again.';
		$redirect = sefBuild($php5WebPath, 'index.php?o=news&m=main', 0);	
		return;
	}
	// User
	$id = php5GetParam($_REQUEST, 'id', 0);	
	$row = new php5News($php5DB);
	$row->load($id);	
	
	if ($row->title=='') {
		$msgAlert = sprintf($lang['_MSS_DEL_NEWS_FAIL'], $row->id);
		$redirect = sefBuild($php5WebPath, 'index.php?o=news&m=main', 0);	
	}	
	
	// Delete user
	$row->delete($id);
	//$msgAlert = sprintf($lang['_MSS_DEL_NEWS_SUCCESS'], $row->id);
	$redirect = sefBuild($php5WebPath, 'index.php?o=news&m=main', 0);	
}
function saveNews(){
	global $smarty, $php5RootAdminPath, $type, $php5Session, $php5WebPath, $php5TemplateAdminFile, $lang, $define, $php5DB, $php5Ext, $php5RootPath;	
	global $msgAlert, $redirect;			
	if(!$define->checkAdminType($php5Session->getVar("admin_type"), "update_news")) {
		$msgAlert='Not allowed to use. Please try again.';
		$redirect = sefBuild($php5WebPath, 'index.php?o=news&m=main', 0);	
		return;
	}
	$id = php5GetParam($_REQUEST, 'news_id', 0);	
	$row = new php5News($php5DB);	
	$row->bind( $_POST );
	if ($id) {
		$row->id = $id;
		$row->update_date  = php5GMTTime();
		$row->update_user  = $php5Session->getVar('admin_name');
	} else {
		$row->post_date = php5GMTTime();
		$row->create_user = $php5Session->getVar('admin_name');
	}
	if (@php5GetParam($_REQUEST, 'delimg', 0)) {		
		unlink($php5ImagePath.'/'.trim(php5GetParam($_REQUEST, 'photo', '')));
		$img = '';
	} else {
		$img = trim(php5GetParam($_REQUEST, 'photo', ''));
	}
	$row->image = $img;
	$row->hot_news = php5GetParam($_REQUEST, 'hot_news', 0);
	if (!$row->store()) {	
		$msgAlert = $lang['_MSS_CANT_SAVE'];		
	} else {	
		//$msgAlert = $lang['_MSS_SAVE_SUCCESSFULL'];
		$redirect = sefBuild($php5WebPath, 'index.php?o=news&m=main', 0);
	}
	
}
function editNews(){	
	global $smarty, $php5RootAdminPath, $type, $php5Session, $php5WebPath, $php5TemplateAdminFile, $lang, $php5DB,$define, $php5RootPath,$php5Ext ;	
	global $msgAlert, $redirect;			
	if(!$define->checkAdminType($php5Session->getVar("admin_type"), "update_news")) {
		$msgAlert='Not allowed to use. Please try again.';
		$redirect = sefBuild($php5WebPath, 'index.php?o=news&m=main', 0);	
		return;
	}	
	$id = php5GetParam($_REQUEST, 'id', 0);	
	$row = new php5News($php5DB);
	$row->load($id);
	$smarty->assign('row', $row);	
	// Category
	$php5DB->setQuery("SELECT id, name FROM #__category WHERE publish>0 AND type=2 ORDER BY `name` ASC");
	$rows = $php5DB->loadObjectList();
	$cats = array();	
	foreach ($rows as $row){
		$cats[$row->id] = stripslashes($row->name);	
	}
	$smarty->assign('cats', $cats);			
	
	$smarty->assign('_NEWS', $lang["_NEWS"]);	
	$smarty->assign('_CATEGORY', $lang["_CATEGORY"]);
	$smarty->assign('_ADD_CATEGORY', $lang["_ADD_CATEGORY"]);
	$smarty->assign('_TITLE', $lang["_TITLE"]);
	$smarty->assign('_IMAGE', $lang["_IMAGE"]);
	$smarty->assign('_SHORT_DESCRIPTION', $lang["_SHORT_DESCRIPTION"]);
	$smarty->assign('_CONTENT', $lang["_CONTENT"]);
	$smarty->assign('_HIGHTLIGHT', $lang["_HIGHTLIGHT"]);
	$smarty->assign('_PUBLISHED', $lang["_PUBLISHED"]);
	$smarty->assign('_YES', $lang["_YES"]);
	$smarty->assign('_NO', $lang["_NO"]);
	
	// Message
	$smarty->assign('_MSS_NEWS_TITLE', $lang["_MSS_NEWS_TITLE"]);
	$smarty->assign('_MSS_NEWS_S_DESC', $lang["_MSS_NEWS_S_DESC"]);
	$smarty->assign('_MSS_NEWS_DESC', $lang["_MSS_NEWS_DESC"]);
	
	$smarty->assign('_UPDATE', $lang["_UPDATE"]);	
	$smarty->assign('_RESET', $lang["_RESET"]);	
	$smarty->assign('_BACK', $lang["_BACK"]);	
	
	$smarty->assign('link_save_user', sefBuild($php5WebPath, 'index.php?o=account&m=main&task=save', 0));	
	
	$smarty->assign('main', $smarty->fetch(sprintf($php5TemplateAdminFile, 'en', $type , 'edit.tpl')));
}
function addNews(){	
	global $smarty, $php5RootAdminPath, $type, $php5Session, $php5WebPath, $php5TemplateAdminFile, $lang, $define, $php5DB;		
	global $msgAlert, $redirect;			
	if(!$define->checkAdminType($php5Session->getVar("admin_type"), "update_news")) {
		$msgAlert='Not allowed to use. Please try again.';
		$redirect = sefBuild($php5WebPath, 'index.php?o=news&m=main', 0);	
		return;
	}
	// Category
	$php5DB->setQuery("SELECT id, name FROM #__category WHERE publish>0 AND type=2 ORDER BY `name` ASC");
	$rows = $php5DB->loadObjectList();
	$cats = array();	
	foreach ($rows as $row){
		$cats[$row->id] = stripslashes($row->name);	
	}
	$smarty->assign('cats', $cats);			
	
	$smarty->assign('_CREATE_NEWS', $lang["_CREATE_NEWS"]);	
	$smarty->assign('_CATEGORY', $lang["_CATEGORY"]);
	$smarty->assign('_ADD_CATEGORY', $lang["_ADD_CATEGORY"]);
	$smarty->assign('_TITLE', $lang["_TITLE"]);
	$smarty->assign('_IMAGE', $lang["_IMAGE"]);
	$smarty->assign('_SHORT_DESCRIPTION', $lang["_SHORT_DESCRIPTION"]);
	$smarty->assign('_CONTENT', $lang["_CONTENT"]);
	$smarty->assign('_HIGHTLIGHT', $lang["_HIGHTLIGHT"]);
	$smarty->assign('_PUBLISHED', $lang["_PUBLISHED"]);
	$smarty->assign('_YES', $lang["_YES"]);
	$smarty->assign('_NO', $lang["_NO"]);
	
	// Message
	$smarty->assign('_MSS_NEWS_TITLE', $lang["_MSS_NEWS_TITLE"]);
	$smarty->assign('_MSS_NEWS_S_DESC', $lang["_MSS_NEWS_S_DESC"]);
	$smarty->assign('_MSS_NEWS_DESC', $lang["_MSS_NEWS_DESC"]);	
	
	$smarty->assign('_CREATE', $lang["_CREATE"]);	
	$smarty->assign('_RESET', $lang["_RESET"]);	
	$smarty->assign('_BACK', $lang["_BACK"]);	
	
	$smarty->assign('main', $smarty->fetch(sprintf($php5TemplateAdminFile, 'en', $type , 'add.tpl')));
}
function listNews(){	
	global $smarty, $php5RootAdminPath, $type, $php5Session, $php5WebPath, $php5TemplateAdminFile, $lang, $define, $php5DB, $allow_news;
	
	$cat_id = intval(php5GetParam($_REQUEST, 'task', 0));	
	$smarty->assign('cat_id', $cat_id);		

	$sWhere = '';
	if ($cat_id)
		$sWhere = " AND n.category_id=".$cat_id;	
	
	// Category
	$php5DB->setQuery("SELECT id, name FROM #__category WHERE publish>0 AND type=2 ORDER BY `name` ASC");
	$rows = $php5DB->loadObjectList();
	$cats = array();	
	foreach ($rows as $row){
		$cats[$row->id] = stripslashes($row->name);	
	}	
	$smarty->assign('cats', $cats);			
	
	//paging
	$currPage = php5GetParam($_REQUEST, 'p', 1);
	$limit = php5ConfigMaxRecords;
	$php5DB->setQuery("SELECT COUNT(n.id)
					FROM #__news AS n
					INNER JOIN #__category AS c ON n.category_id = c.id
					WHERE 1 ".$sWhere." 
					ORDER BY n.ordering ASC, n.id ASC");
	$recordTotal = $php5DB->loadResult();
	
	$startLimit= ($currPage - 1) * $limit;
	
	$pageTotal = ceil($recordTotal / $limit);
	$currPage = $currPage > $pageTotal ? $pageTotal : $currPage;
	$paging = new php5Paging($pageTotal, $currPage);
	$paging->url = sefBuild($php5WebPath, 'index.php?o=news&m=main', 0).'?';
	$smarty->assign('paging', $paging->showDivPages());
	
	// News list
	$php5DB->setQuery("SELECT n.*, c.name
					FROM #__news AS n
					INNER JOIN #__category AS c ON n.category_id = c.id
					WHERE 1 ".$sWhere." 
					ORDER BY n.ordering ASC, n.id ASC 
					LIMIT $startLimit, $limit");	
	$rows= $php5DB->loadObjectList();
	foreach ($rows as $row){
		$row->post_date = date("m.j.Y", $row->post_date);
	}
	$smarty->assign('rows', $rows);		
	
	$smarty->assign('_NEWS', $lang["_NEWS"]);		
	$smarty->assign('_SELECT_USER_TYPE', $lang["_SELECT_USER_TYPE"]);	
	$smarty->assign('_TITLE', $lang["_TITLE"]);
	$smarty->assign('_HIGHTLIGHT', $lang["_HIGHTLIGHT"]);
	$smarty->assign('_PUBLISHED', $lang["_PUBLISHED"]);
	$smarty->assign('_CATEGORY', $lang["_CATEGORY"]);	
	$smarty->assign('_AUTHOR', $lang["_AUTHOR"]);
	$smarty->assign('_DATE', $lang["_DATE"]);
	$smarty->assign('_DATE', $lang["_DATE"]);
	$smarty->assign('_HITS', $lang["_HITS"]);	
	$smarty->assign('_ORDER', $lang["_ORDER"]);	
	
	
	$smarty->assign('_DELETE', $lang["_DELETE"]);
	$smarty->assign('_EDIT', $lang["_EDIT"]);
	$smarty->assign('_ADD_NEWS', $lang["_ADD_NEWS"]);
	$smarty->assign('_ADD_CATEGORY', $lang["_ADD_CATEGORY"]);
	
	$smarty->assign('_MSS_DEL_NEWS', $lang["_MSS_DEL_NEWS"]);	

	$smarty->assign('main', $smarty->fetch(sprintf($php5TemplateAdminFile, 'en', $type , 'list.tpl')));
	
}